const http = require("http");
const fs = require("fs");
const url = require("url");
const myurl = require("./myurl");
const server = http.createServer((req, res) => {
  res.setHeader("Content-type", "text/html;charset=utf-8");
  let { pathname } = url.parse(req.url);

  if (pathname === "/") {
    let data = fs.readFileSync("./register.html", "utf-8");
    res.write(data);
    res.end();
  } else if (pathname === "/register") {
    let obj = myurl.parse(req.url);

    fs.writeFileSync("./user.json", JSON.stringify(obj));

    res.write(`已把注册的信息通过fs模块保存到user.json里<br><a href="/show">点击查看注册的用户信息</a>`);
    res.end();
  } else if (pathname === "/show") {
    // 显示注册的用户信息

    let users = JSON.parse(fs.readFileSync("./user.json", "utf-8"));

    res.write(`用户名是${users.username}<br>密码是${users.pwd}`);
    res.end();
  }
});
server.listen(8989);
